<?php
/**
 * Created by : PhpStorm
 * Message: 后台首页控制器
 * User: bingdi
 * Time: 2020/2/1814:42
 * motto: 现在的努力是为了小时候吹过的牛逼!
 */

namespace app\admin\controller;


use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\route\dispatch\Controller;

class Article extends AdminBase
{
    public function index()
    {
        $model=Db::name('article')
            ->select()
            ->toArray();
        return View::fetch("article_list",['model'=>$model]);
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            $data['content']=$data['editorValue'];
            unset($data['editorValue']);
            if ($data['id']) {
                $res = Db::name('article')
                    ->where('id', $data['id'])
                    ->update($data);
            } else {
                $res = Db::name('article')
                    ->insert($data);
            }
            if (!$res) {
                return show(0, '数据保存失败');
            }
            return show(1, '数据保存成功');
        } else {
            $id = input('id');
            $model = Db::name('article')
                ->where('id', $id)
                ->find();
//        dd($type);
            return View::fetch('article_add', ['model' => $model]);
        }
    }

}